package com.zprmb.udzt;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class RealName extends BaseActivity implements OnClickListener{
	private EditText et_name,et_idnum;
	private Button btn_comnfirm;
	private ImageView img_back;
	private RelativeLayout rl_back;
	private TextView tv_customer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.real_name);
		 BbtApplication.getInstance().addActvity(this);
		et_name=(EditText)findViewById(R.id.et_name);
		et_idnum=(EditText)findViewById(R.id.et_idnum);
		btn_comnfirm=(Button)findViewById(R.id.btn_confirm);
		img_back=(ImageView)findViewById(R.id.img_back);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		tv_customer=(TextView)findViewById(R.id.enterprise_tel);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		img_back.setOnClickListener(this);
		btn_comnfirm.setOnClickListener(this);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initData() {
		// TODO Auto-generated method stub
		final AjaxParams params=new AjaxParams(); 
		try {
			params.put("userName", URLEncoder.encode(et_name.getText().toString(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		params.put("idNumber", et_idnum.getText().toString());
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if(jsonObject.optInt("success")==1){
					JSONObject json=jsonObject.optJSONObject("data");
					Log.e("实名", jsonObject.toString());
					Toast.makeText(RealName.this, jsonObject.optString("comment"), 2000).show();
					Log.e("sss", params.toString());
					Intent intent1=new Intent(RealName.this,RealNameSuccess.class);
//					intent1.putExtra("bind",json.optString("bindcardAmount"));
//					intent1.putExtra("auth",json.optString("authenticationAmount"));
					startActivity(intent1);
					RealName.this.finish();
				}else{
					Toast.makeText(RealName.this, jsonObject.optString("comment"), 2000).show();
				}
				
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.execute(RealName.this, Constant.URL+"/api/v2/idAuth", "?"+params.toString());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btn_confirm:
			if(et_name.getText().toString().equals("")){
				Toast.makeText(RealName.this, "请填写真实姓名", 2000).show();
				return;
			}
			if(et_idnum.getText().toString().equals("")){
				Toast.makeText(RealName.this, "请填写真实身份证信息", 2000).show();
				return;
			}
			initData();
		
			break;
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;

		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
	}

}
